/*
	Copyright (C) 2008 by Jean-François Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/


#include "all.h"
#include "joystick.h"


SDL_Joystick *joystick;


void initializeJoystick(void) {

    /*PRINTF("%i joysticks were found.\n\n", SDL_NumJoysticks() );
    PRINTF("The names of the joysticks are:\n");
*/
    for( int i=0; i < SDL_NumJoysticks(); i++ )
    {
        PRINTF("    %s\n", SDL_JoystickName(i));
    }


    if (SDL_NumJoysticks() > 0) {
        joystick = SDL_JoystickOpen  (0);
        /*PRINTF("Number of Axes: %i\n", SDL_JoystickNumAxes(joystick));
        PRINTF("Number of Buttons: %i\n", SDL_JoystickNumButtons(joystick));
        PRINTF("Number of Balls: %i\n", SDL_JoystickNumBalls(joystick));
        PRINTF("Number of Hats: %i\n", SDL_JoystickNumHats(joystick));*/
    }
    else
    joystick = NULL;

}


void getJoystickButtonPressed(bool *up, bool *down, bool *left, bool *right, bool *button1, bool *button2, bool *button3) {

    SDL_JoystickUpdate();

    int x_move, y_move;
    x_move=SDL_JoystickGetAxis(joystick, 0);
    y_move=SDL_JoystickGetAxis(joystick, 1);


    if (left!=NULL) {
        if (x_move<-30000)
        *left = true;
        else
        *left = false;
    }

    if (right!=NULL) {
        if (x_move>30000)
        *right = true;
        else
        *right = false;
    }

    if (down!=NULL) {
        if (y_move>30000)
        *down = true;
        else
        *down = false;
    }

    if (up!=NULL) {
        if (y_move<-30000)
        *up = true;
        else
        *up = false;
    }

    PRINTF("y:%i\n",y_move);

    if (button1!=NULL) {
        if (SDL_JoystickGetButton  (joystick, 0) || SDL_JoystickGetButton  (joystick, 3))
        *button1 = true;
        else
        *button1 = false;
    }

    if (button2!=NULL) {
        if (SDL_JoystickGetButton  (joystick, 1) | SDL_JoystickGetButton  (joystick, 2))
        *button2 = true;
        else
        *button2 = false;
    }

}

bool hasPressedJoystickEnterButton(void) {
    if (joystick==NULL)
    return false;
    SDL_JoystickUpdate();
    if (SDL_JoystickGetButton  (joystick, 0) || SDL_JoystickGetButton  (joystick, 3) || SDL_JoystickGetButton  (joystick, 1) | SDL_JoystickGetButton  (joystick, 2))
    return true;
    return false;
}
